package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysQuestion;
import com.ruoyi.system.service.ISysQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题目信息Controller
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/question")
public class SysQuestionController extends BaseController {
    private final ISysQuestionService sysQuestionService;

    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysQuestion sysQuestion) {
        startPage();
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "题目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysQuestion sysQuestion) {
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        ExcelUtil<SysQuestion> util = new ExcelUtil<SysQuestion>(SysQuestion.class);
        util.exportExcel(response, list, "题目信息数据");
    }

    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysQuestionService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "题目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysQuestion sysQuestion) {
        return toAjax(sysQuestionService.save(sysQuestion));
    }

    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "题目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysQuestion sysQuestion) {
        return toAjax(sysQuestionService.updateById(sysQuestion));
    }

    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "题目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysQuestionService.removeBatchByIds(Arrays.asList(ids)));
    }
}
